package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.FinanceDao;
import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.domain.cargo.Finance;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.PackingList;
import cn.itcast.service.cargo.FinanceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private FinanceDao financeDao;

    @Autowired
    private InvoiceDao invoiceDao;

    @Override
    public void save(Finance finance) {
        finance.setCreateTime(new Date());
        financeDao.insert(finance);

        Invoice invoice = invoiceDao.selectById(finance.getInvoiceId());

        invoice.setStatus(1);

        invoiceDao.updateById(invoice);

    }

    @Override
    public PageInfo<Finance> findByPage(Integer pageNum, Integer pageSize, String loginUserCompanyId) {
        PageHelper.startPage(pageNum,pageSize);

        List<Finance> financeList = financeDao.selectList(new QueryWrapper<Finance>().eq("company_id", loginUserCompanyId));

        PageInfo<Finance> pageInfo = new PageInfo<>(financeList);

        return pageInfo;
    }

    @Override
    public Finance findById(String id) {
        return financeDao.selectById(id);
    }
}
